/************************************方案区域********************************************************/
// 保存方案基础信息
function  saveHpinfo(){
    var toUrl = ctx + "/datamanage/addOneHp.do";
    var hpname =  $('#hpname').val();
    var hpcontent  =  $('#hpcontent').val();
    var hptype  =  $('#hptype').val();
    if(isNull(hpname)){
    	alert("调养方案名称不能为空！");
    	return;
    }
    if(isNull(hptype)){
    	alert("请选择调养方案类型！");
    	return;
    }
    if(isNull(hpcontent)){
    	alert("调养方案内容不能为空！");
    	return;
    }
    
	$("#modelForm").ajaxSubmit({
		 url: toUrl,
		 type : 'post',
		 dataType: 'text',
		 beforeSend:function(data){   
	          
	           
	     },//发送请求前   
		 success: function (data) {
		 	if("-1" == data){
		 		alert("该调养方案已存在！");
		 	}
		 	$("#id").val(data); // 设置方案主键
		 	alert("保存成功！"); 
		 	// 显示其他操作区域
			$("#hp-otherinfo").css("display", "block");
         },
         error : function(data){
         	alert("保存失败！");  	 
         }
	 });
}

/************************************功效区域********************************************************/
//功效下拉选择
function dropEffectList(){
	// 移除旧区
	$("#effect").remove();
	$("#dss").remove();
	
	// 显示加载
	var html = "";
	html += "<div id='effect' class='select-menubutton' style='width:430px;'>";
	html += 	"<div class='overflow_warp'>";
	html += 		"<div class='msg'>加载中...</div>";
	html += 	"</div>";
	html += "</div>";
	
	// 显示下拉
	$("#effectT").after(html);
	var targetId = "effectSearch";
	var targetOffset = $("#" + targetId).offset();
	$("#effect").offset({left:targetOffset.left});
	
	// 获取数据
	var params = {};
	params.effectname = $("#effectSearch").val();
	params.hpid = $("#id").val();
	$.ajax({
		url: ctx + "datamanage/loadFilterEffects.do",
		data: params,
		dataType: "json",
		type: "post",
		async:false,
		cache:false,
		success: function(data){
			// 无结果
			if(0 == data.list.length){
				$('[class=msg]').text('没有找到功效');
				$('[class=msg]').append('<br/><input type="button" class="btn btn-primary" value="创建功效" onclick="saveEffect();" />');
				return;
			};
			
			// 生成下拉列表
			var table = "";
			table += 		"<table class='inner-table'>";
			table += 			"<thead>";
			table +=					"<tr><th width='30px'></th><th width='200px'>功效名称</th><th width='200px'>功效类别</th></tr>"
		    table += 			"<thead>";
			table +=			"<tbody>";
			$(data.list).each(function(index,result){
				var checked = "";
				if(result.checked == "1"){
					checked = "checked='checked'";
				}
				table +=  "<tr><td><input id='ck_effect" + result.id + "' " + checked + " type='checkbox' value='"+result.id+"' title='" + result.effectname + "'></td><td>" + result.effectname + "</td><td>" + result.type + "</td></tr>"
			});
			table +=			"</tbody>"
			table += 		"</table>";
			$('[class=msg]').remove();
			$('[class=overflow_warp]').append($(table));
			
			
			// 复选框事件
			$("#effect input:checkbox").each(function(index,obj){
				$(obj).bind('click',function(evt){
					var effectid = $(this).val();
					if(!$(this).attr('checked')){ // 取消
						deleteRelationEffect(effectid);
					}else{ // 选中
						saveRelationEffect(effectid,$(this).attr("title"));
					}
					evt.stopPropagation();
				});
			});
			
			// 行选择点击事件
			$("#effect tbody tr").each(function(index,obj){
				$(this).bind("click",function(evt){
					var $checkbox = $(this).find("input:checkbox:eq(0)");
					var effectid = $checkbox.val();
					if(!$checkbox.attr('checked')){ // 选中
						$checkbox.attr('checked',true);
						saveRelationEffect(effectid,$checkbox.attr("title"));
					}else{ // 取消
						$checkbox.attr('checked',false);
						deleteRelationEffect(effectid);
					}
					evt.stopPropagation();
				});
			}); 
		}
	});
	$("#effect").bind("click",function(evt){
		evt.stopPropagation();
	});
}

//弹出新增功效
function saveEffect(){
	var fromUrl = ctx + "app/data/insert-one-effect.jsp";
	var toUrl = ctx + "datamanage/addOneEffect.do";
	var title = "添加功效信息";
	Tool.showCallbackDialog(fromUrl,toUrl,title,600,200,function(){
		dropEffectList();
	});
}

// 保存功效
function saveRelationEffect(effectid,effectname){
	$('[class=overflow_warp]').block({ message: null }); 
	var params = {
		"rid" : $("#id").val(),
		"effectid" : effectid,
		"type": 1
	};
	$.ajax({
		url: ctx + "datamanage/saveEffectRelation.do",
		data: params,
		dataType: "text",
		type: "post",
		async:false,
		cache:false,
		success: function(data){
			$('#effectlist').append('<li id=\'effect' + effectid + '\' name=\'effect' + effectid + '\'>' + effectname + '<a href="javascript:void(0);" onclick="deleteRelationEffect(\'' + effectid + '\');">删除</a></li>');
		},
        error : function(data){
         	alert("保存失败！");  	 
        }
	});
	$('[class=overflow_warp]').unblock();
}

// 删除功效
function deleteRelationEffect(effectid){
	$('[class=overflow_warp]').block({ message: null });
	var params = {
			"rid" : $("#id").val(),
			"effectid" : effectid,
			"type": 1
		};
	$.ajax({
		url: ctx + "datamanage/deleteEffectRelation.do",
		data: params,
		dataType: "text",
		type: "post",
		async:false,
		cache:false,
		success: function(data){
			$('#effect' + effectid).remove();
		},
        error : function(data){
         	alert("删除失败！");  	 
        }
	});
	$('[class=overflow_warp]').unblock();
}


/************************************病症区域*******************************************************/
// 病症下拉列表
function dropDssList(){
	// 移除旧区
	$("#effect").remove();
	$("#dss").remove();
	
	// 显示加载
	var html = "";
	html += "<div id='dss' class='select-menubutton' style='width:630px;'>";
	html += 	"<div class='overflow_warp'>";
	html += 		"<div class='msg'>加载中...</div>";
	html += 	"</div>";
	html += "</div>";
	
	// 显示下拉
	$("#dssT").after(html);
	var targetId = "dssSearch";
	var targetOffset = $("#" + targetId).offset();
	$("#dss").offset({left:targetOffset.left});
	
	// 获取数据
	var params = {};
	params.dssname = $("#dssSearch").val();
	params.hpid = $("#id").val();
	$.ajax({
		url: ctx + "datamanage/loadFilterDss.do",
		data: params,
		dataType: "json",
		type: "post",
		async:false,
		cache:false,
		success: function(data){
			// 无结果
			if(0 == data.list.length){
				$('[class=msg]').text('没有找到病症');
				$('[class=msg]').append('<br/><input type="button" class="btn btn-primary" value="创建病症" onclick="saveDss();" />');
				return;
			};
			
			// 创建下拉表
			var table = "";
			table += 		"<table class='inner-table'>";
			table += 			"<thead>";
			table +=					"<tr><th width='30px'></th><th width='200px'>类别</th><th width='200px'>名称</th><th width='200px'>分类</th></tr>"
		    table += 			"<thead>";
			table +=			"<tbody>";
			$(data.list).each(function(index,result){
				var checked = "";
				if(result.checked == "1"){
					checked = "checked='checked'";
				}
				var addStatus = result.type;
				table += "<tr>";
				table +=	"<td><input id='ck_dss" + result.id + "' " + checked + " type='checkbox' value='"+result.id+"' title='" + result.dssname + "'></td>";
				if(addStatus == 1){ // 适应症
					table +=	"<td><input type='radio' id='r_dss_" + result.id + "1' name='r_dss_" + result.id + "' checked='checked' value='1' _value='" + result.id + "'>&nbsp;&nbsp;<label for='r_dss_" + result.id + "1'>适应症</label>&nbsp;&nbsp;&nbsp;&nbsp;<input id='r_dss_" + result.id + "2' type='radio' name='r_dss_" + result.id + "' _value='" + result.id + "' value='2'>&nbsp;&nbsp;<label for='r_dss_" + result.id + "2'>禁忌症<label></td>";
				}else if(addStatus == 2){ // 禁忌症
					table +=	"<td><input type='radio' id='r_dss_" + result.id + "1' name='r_dss_" + result.id + "' value='1' _value='" + result.id + "' />&nbsp;&nbsp;<label for='r_dss_" + result.id + "1'>适应症</label>&nbsp;&nbsp;&nbsp;&nbsp;<input id='r_dss_" + result.id + "2' type='radio' name='r_dss_" + result.id + "' checked='checked' value='2' _value='" + result.id + "'/>&nbsp;&nbsp;<label for='r_dss_" + result.id + "2'>禁忌症<label></td>";
				}else{
					table +=	"<td><input type='radio' id='r_dss_" + result.id + "1' name='r_dss_" + result.id + "' value='1' _value='" + result.id + "'>&nbsp;&nbsp;<label for='r_dss_" + result.id + "1'>适应症</label>&nbsp;&nbsp;&nbsp;&nbsp;<input id='r_dss_" + result.id + "2' type='radio' name='r_dss_" + result.id + "' _value='" + result.id + "' value='2'>&nbsp;&nbsp;<label for='r_dss_" + result.id + "2'>禁忌症<label></td>";
				}
				table += 	"<td>" + result.dssname + "</td><td>" + result.catagory + "</td>";
				table += "</tr>";
			});
			table +=			"</tbody>"
			table += 		"</table>";
			$('[class=msg]').remove();
			$('[class=overflow_warp]').append($(table));
			
			
			// 复选框事件
			$("#dss input:checkbox").each(function(index,obj){
				$(obj).bind('click',function(evt){
					var dssid = $(this).val();
					if(!$(this).attr('checked')){ // 取消
						deleteRelationDss(dssid);
						$(this).parent().parent().find("input:radio:checked").attr("checked",false);
					}else{ // 选中
						var type = $(this).parent().parent().find("input:radio:checked").val(); // 获取症型
						if(null == type || undefined == type){
							$(this).parent().parent().find("input:radio").eq(0).attr("checked",true);
							type = 1;
						}
						saveRelationDss(dssid,$(this).attr("title"),type);
					}
					evt.stopPropagation();
				});
			});
			
			// 单选按钮切换
			$("#dss input:radio").bind("change",function(evt){
				var $checked = $(this).parent().parent().find("input:checkbox");
				var checked = $checked.attr("checked");
				var dssname = $checked.attr("title");
				var dssid = $checked.val();
				var type = $(this).val();
				if(checked && $('#dsslist' + dssid).find("#dss" + dssid).size() == 0){ // 选中时且未保存
					changeDssType(dssid,dssname,type);
				}
				evt.stopPropagation();
			});
			
			// 行选择点击事件
			$("#dss tbody tr").each(function(index,obj){
				$(this).bind("click",function(evt){
					var $checkbox = $(this).find("input:checkbox:eq(0)");
					var dssid = $checkbox.val();
					if(!$checkbox.attr('checked')){ // 选中
						$checkbox.attr('checked',true);
						var type = $(this).find("input:radio:checked").val(); // 获取症型
						if(null == type || undefined == type){
							$(this).find("input:radio").eq(0).attr("checked",true);
							type = 1;
						}
						saveRelationDss(dssid,$checkbox.attr("title"),type);
					}else{ // 取消
						$checkbox.attr('checked',false);
						$(this).find("input:radio:checked").attr("checked",false);
						deleteRelationDss(dssid);
					}
					evt.stopPropagation();
				});
				
				// 解除TD冒泡
				$(this).find("td:eq(1)").bind("click",function(evt){
					evt.stopPropagation();
				});
			}); 
		}
	});
	$("#dss").bind("click",function(evt){
		evt.stopPropagation();
	});
}

//弹出新增病症
function saveDss(){
	var fromUrl = ctx + "app/data/insert-one-dss.jsp";
	var toUrl = ctx + "datamanage/addOneDss.do";
	var title = "添加病症证信息";
	Tool.showCallbackDialog(fromUrl,toUrl,title,800,600,function(){
		dropDssList();
	});
}

// 选取病症
function saveRelationDss(dssid,dssname,type){
	$('[class=overflow_warp]').block({ message: null });
	var params = {
			"rid" : $("#id").val(),
			"dssid" : dssid,
			"rtype": 1,
			"type": type
		};
		$.ajax({
			url: ctx + "datamanage/saveDssRelation.do",
			data: params,
			dataType: "text",
			type: "post",
			async:false,
			cache:false,
			success: function(data){
				$("#dss" + dssid).remove();
				$('#dsslist' + type).append('<li id=\'dss' + dssid + '\' name=\'dss' + dssid + '\'>' + dssname + '<a href="javascript:void(0);" onclick="deleteRelationDss(\'' + dssid + '\');">删除</a></li>');
			},
	        error : function(data){
	         	alert("保存失败！");  	 
	        }
		});
	$('[class=overflow_warp]').unblock();
}

// 删除病症
function deleteRelationDss(dssid){
	$('[class=overflow_warp]').block({ message: null });
	var params = {
		"rid" : $("#id").val(),
		"dssid" : dssid,
		"rtype": 1
	};
	$.ajax({
		url: ctx + "datamanage/deleteDssRelation.do",
		data: params,
		dataType: "text",
		type: "post",
		async:false,
		cache:false,
		success: function(data){
			$("#dss" + dssid).remove();
		},
        error : function(data){
         	alert("删除失败！");  	 
        }
	});
	$('[class=overflow_warp]').unblock();
}

// 变更症状
function changeDssType(dssid,dssname,type){
	$('[class=overflow_warp]').block({ message: null });
	var params = {
		"rid" : $("#id").val(),
		"dssid" : dssid,
		"rtype": 1,
		"type": type
	};
	$.ajax({
		url: ctx + "datamanage/updateDssRelation.do",
		data: params,
		dataType: "text",
		type: "post",
		async:false,
		cache:false,
		success: function(data){
			$("#dss" + dssid).remove();
			$('#dsslist' + type).append('<li id=\'dss' + dssid + '\' name=\'dss' + dssid + '\'>' + dssname + '<a href="javascript:void(0);" onclick="deleteRelationDss(\'' + dssid + '\');">删除</a></li>');
		},
        error : function(data){
         	alert("变更失败！");  	 
        }
	});
	$('[class=overflow_warp]').unblock();
}